package com.baor.service.impl;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.MenuDao;
import com.baor.dao.RoleDao;
import com.baor.dao.Role_MenuDao;
import com.baor.domain.CustomerVO;
import com.baor.domain.MenuVO;
import com.baor.domain.RoleVo;
import com.baor.domain.Role_MenuVo;
import com.baor.service.RoleService;
import com.baor.util.R;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.security.Permission;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public R roleList(Map<String, Object> params) {

        int pageIndex = (int) params.get("pageIndex");
        int pageSize = (int) params.get("pageSize");
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        List<RoleVo> roleVos = this.menuDao.roleList(params);
        Map<String, Object> map = this.menuDao.pageCount();
        //逗号分割
        roleVos.forEach(roleVo -> {
            String collect = roleVo.getMenus().stream().map(
                    x -> x.getTitle()).collect(Collectors.joining(","));
            roleVo.setMenuNames(collect);
            roleVo.setMenus(null);
        });
        //TODO 求页码
        map.put("roleList", roleVos);

        return R.ok(map);
    }

    @Override

    public R addRole(Map<String, Object> params) {
        RoleVo roleVo = new RoleVo();
        roleVo.setName(params.get("name").toString());
        roleVo.setDescription(params.get("description").toString());
        roleVo.setStatus(params.get("status").toString());
        List<Integer> menuIds = (List<Integer>) params.get("menuIds");
        int row = this.roleDao.insert(roleVo);
        if(row > 0){
            Integer id = roleVo.getId();
            params.clear();
            params.put("roleId",id);
            params.put("menuIds",menuIds);
            int i = this.menuDao.addRoleMenu(params);
            return i > 0 ? R.ok() : R.er();
        }


        return null;
    }

    @Override
    public R updateRole(Map<String,Object> params){
        //根据roleIds修改role选项
        System.out.println(params);
        if(params.get("name") != null || params.get("description") != null || params.get("status") != null){
            //修改菜单信息
            LambdaUpdateWrapper<RoleVo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(params.get("name") != null,RoleVo::getName,String.valueOf(params.get("name")));
            updateWrapper.set(params.get("description") != null,RoleVo::getDescription,String.valueOf(params.get("description")));
            updateWrapper.set(params.get("status") != null,RoleVo::getStatus,String.valueOf(params.get("status")));

            updateWrapper.eq(RoleVo::getId,Integer.parseInt(params.get("id").toString()));
            int update = this.roleDao.update(updateWrapper);
        }
        int id = Integer.parseInt(params.get("id").toString());
        int row = this.menuDao.deleteRoleMenu(id);
        if(row > 0){
            List<Integer> menuIds = (List<Integer>) params.get("menuIds");
            params.clear();
            params.put("roleId",id);
            params.put("menuIds",menuIds);
            int i = this.menuDao.addRoleMenu(params);
            return i > 0 ? R.ok() : R.er();
        }
        return R.er();
    }
}
